﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Adduser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)//初次加载
        {
            if (Session["name"] == null)
            {
                Response.Write("<script>alert('非法登录!!!');window.top.location='../Login.aspx';</script>");
                return;
            }
            else
            {
                BindAlter();
            }
          
        }
    }

    //添加 修改用户
    protected void btn_add_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txt_name.Text) || string.IsNullOrEmpty(txt_pwd.Text))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('用户名和密码不能为空！')</script>");
            return;
        }
        if (txt_pwd.Text.Trim() != txt_pwds.Text.Trim())
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('两次输入密码不一致！请重新输入！')</script>");
            return;
        }
        if (btn_add.Text == "添加")
        {
            string insertstr = "insert into tb_admin (a_name,a_pwd) values (@name,@pwd)";
            OleDbParameter[] param ={
                                       new OleDbParameter("@name",txt_name.Text.Trim()),
                                      // new OleDbParameter("@pwd",Common.Md5coding(txt_pwd.Text.Trim())),
                                      new OleDbParameter("@pwd",txt_pwd.Text.Trim()),
                                   };
            int flag = OleDbHelper.ExecuteNonSql(insertstr, param);
            if (flag > 0)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('添加成功！')</script>");
                txt_name.Text = "";
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('添加失败！')</script>");
            }
        }
        if (btn_add.Text == "修改")
        {
            string insertstr = "update tb_admin set a_name=@name,a_pwd=@pwd where a_id=@id";
            OleDbParameter[] param ={
                                       new OleDbParameter("@name",txt_name.Text.Trim()),
                                       //new OleDbParameter("@pwd",Common.Md5coding(txt_pwd.Text.Trim())),
                                       new OleDbParameter("@pwd",txt_pwd.Text.Trim()),
                                       new OleDbParameter("@id",int.Parse(Request.QueryString["uid"].ToString()))
                                   };
            int flag = OleDbHelper.ExecuteNonSql(insertstr, param);
            if (flag > 0)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改成功！')</script>");
                txt_name.Text = "";
                btn_add.Text = "添加";
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改失败！')</script>");
            }
        }
    }

    //判断修改还是添加
    protected void BindAlter()
    {
        if (Request.QueryString["uid"] != null)
        {
            int id = int.Parse(Request.QueryString["uid"].ToString());
            string str = "select * from tb_admin where a_id=@uid";
            OleDbParameter[] param = { 
                                         new OleDbParameter("@uid",id)
                                     };
            OleDbDataReader dr = OleDbHelper.GetDataReader(str, param);
            if (dr.Read())
            {
                txt_name.Text = dr["a_name"].ToString();
            }
            dr.Close();
            btn_add.Text = "修改";
        }
        else
        {
            btn_add.Text = "添加";
        }
    }


    //清除
    protected void btn_clear_Click(object sender, EventArgs e)
    {
        txt_name.Text = "";
        txt_pwd.Text = "";
        txt_pwds.Text = "";
        btn_add.Text = "添加";
    }
}